package model;

import j_util.comparators.StringComparator;
import j_util.sorters.BubbleSort;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * @author Jose Martin Rozanec;
 */
public class MarksExportManager {
    private static final String institutions = "institutions";

    public static List<String> getInstitutionsList() {
        List<String> list = null;
        try {
            RandomAccessFile raf = new RandomAccessFile(institutions, "rw");
            String institutions = raf.readUTF();
            String[] array = institutions.split(",");
            list = new ArrayList<String>();
            for (int j = 0; j < array.length; j++) {
                list.add(array[j]);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static void addInstitution(String institution) {
        List<String> list = getInstitutionsList();
        list.add(institution);
        BubbleSort sorter = new BubbleSort();
        sorter.sort(list, new StringComparator());
        StringBuffer string = new StringBuffer();
        try {
            RandomAccessFile raf = new RandomAccessFile(institutions, "rw");
            for (int j = 0; j < list.size() - 1; j++) {
                string.append(list.get(j) + ",");
            }
            string.append(list.get(list.size() - 1));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        new File(institutions).delete();
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile(institutions, "rw");
            raf.writeUTF(string.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void setup(boolean deleteFile) {
        if ((!new File(institutions).exists()) || deleteFile) {
            if (deleteFile) {
                new File(institutions).delete();
            }
            RandomAccessFile raf = null;
            try {
                raf = new RandomAccessFile(institutions, "rw");
                raf.writeUTF("Universidad Austral");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
